﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SC.CodeBuilder
{
    partial class Builder
    {
        #region 生成BLL
        /// <summary>
        /// 生成BLL
        /// </summary>
        /// <returns></returns>
        public StringBuilder BuildeBLLSB()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Data.SqlClient;");
            sb.AppendLine();
            if (GetIDAL)
            {
                sb.Append("using ").Append(unameSpace).Append("IDAL;").AppendLine();
            }
            sb.Append("namespace ").Append(unameSpace).AppendLine("BLL");
            sb.AppendLine("{");
            sb.Append("    public partial class ").Append(tableName).AppendLine("");
            sb.AppendLine("    {");
            if (GetIDAL)
            {
                sb.Append("        protected readonly ").Append(unameSpace).Append("I").Append(tableName).Append(" dal = (").Append(unameSpace).Append("I").Append(tableName).Append(unameSpace).Append(")Factory.Create(\"").Append(tableName).AppendLine("\");");
            }
            else
            {
                sb.Append("        private readonly ").Append(unameSpace).Append("DAL.").Append(tableName).Append(" dal = new ").Append(unameSpace).Append("DAL.").Append(tableName).AppendLine("();");
            }
            sb.Append("        public ").Append(tableName).AppendLine("() { }");
            sb.AppendLine();

            if (Add) { BuilderBllAdd(sb); }
            if (Delete) { BuilderBllDelete(sb); }
            if (Update) { BuilderBllUpdate(sb); }
            if (GetModel) { BuilderBllGetModel(sb); }
            if (GetList) { BuilderBllGetList(sb); }
            if (Exists) { BuilderBllExists(sb); }
            if (GetMaxID) { BuilderBllGetMaxId(sb); }
            if (GetRecordCount) { BuilderBllGetRecordCount(sb); }

            sb.AppendLine("    }");
            sb.AppendLine("}");
            return sb;
        }
        #endregion

        #region 增加一条数据 - void BuilderBllAdd(StringBuilder sb)
        /// <summary>
        /// 增加一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllAdd(StringBuilder sb)
        {
            sb.AppendLine("        #region 增加一条数据");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 增加一条数据");
            sb.AppendLine("        /// </summary>");
            if (hasidentity)
            {
                sb.Append("        public Model.").Append(tableName).Append(" Add(Model.").Append(tableName).AppendLine(" model)");
                sb.AppendLine("        {");
                sb.AppendLine("            return dal.Add(model);");
            }
            else
            {
                sb.Append("        public bool Add(").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" model)");
                sb.AppendLine("        {");
                sb.AppendLine("            return dal.Add(model) > 0;");
            }
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 更新一条数据 - void BuilderBllUpdate(StringBuilder sb)
        /// <summary>
        /// 更新一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllUpdate(StringBuilder sb)
        {
            sb.AppendLine("        #region 更新一条数据");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 更新一条数据");
            sb.AppendLine("        /// </summary>");
            sb.Append("        public bool Update(").Append(unameSpace).Append("Model.").Append(tableName).AppendLine(" model)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.Update(model);");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 删除一条数据 - void BuilderBllDelete(StringBuilder sb)
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllDelete(StringBuilder sb)
        {
            sb.AppendLine("        #region 删除一条数据");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 删除一条数据");
            sb.AppendLine("        /// </summary>");
            if (hasidentity)//自增&主键
            {
                sb.Append("        public bool Delete(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.AppendLine("        {");
                sb.Append("            return dal.Delete(").Append(identityName).AppendLine(");");
                sb.AppendLine("        }");
            }
            else//无主键
            {
                sb.Append("        public bool Delete(Model.").Append(tableName).AppendLine(" model)//自增主键 全无");
                sb.AppendLine("        {");
                sb.AppendLine("            return dal.Delete(model);");
                sb.AppendLine("        }");
            }
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 得到一个对象实体 - void BuilderBllGetModel(StringBuilder sb)
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllGetModel(StringBuilder sb)
        {
            #region 得到一个对象实体
            sb.AppendLine("        #region 得到一个对象实体");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        ///  得到一个对象实体");
            sb.AppendLine("        /// </summary>");
            if (hasidentity)//自增&主键
            {
                sb.Append("        public ").Append(unameSpace).Append("Model.").Append(tableName).Append(" GetModel(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.AppendLine("        {");
                sb.Append("            return dal.GetModel(").Append(identityName).AppendLine(");");
                sb.AppendLine("        }");
            }
            else //无主键
            {
                sb.Append("        public ").Append(unameSpace).Append("Model.").Append(tableName).Append(" GetModel(");
                JoinDrWithType(sb, false);
                sb.AppendLine(")//自增主键 全无");
                sb.AppendLine("        {");
                sb.Append("            return dal.GetModel(");
                JoinDr(sb, false);
                sb.AppendLine(");");
                sb.AppendLine("        }");
            }
            sb.AppendLine("        #endregion");
            sb.AppendLine();
            #endregion
        }
        #endregion

        #region 获得数据列表 - void BuilderBllGetList(StringBuilder sb)
        /// <summary>
        /// 获得数据列表
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllGetList(StringBuilder sb)
        {
            sb.AppendLine("        #region 获得数据列表");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 获得数据列表");
            sb.AppendLine("        /// </summary>");
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels()");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetModels(\"\", 0, \"\");");
            sb.AppendLine("        }");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 获得数据列表");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"strWhere\">Where条件</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, params SqlParameter[] parameters)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetModels(strWhere, 0, null, parameters);");
            sb.AppendLine("        }");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 获得数据列表");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"top\">Top 条件</param>");
            sb.AppendLine("        /// <param name=\"strWhere\">Where 条件</param>");
            sb.AppendLine("        /// <param name=\"order\">Order 条件</param>");
            sb.AppendLine("        /// <param name=\"parameters\">参数</param>");
            sb.AppendLine("        /// <returns></returns>");
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, int top, string order, params SqlParameter[] parameters)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetModels(strWhere, top, order, parameters);");
            sb.AppendLine("        }");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 分页获取数据列表");
            sb.AppendLine("        /// </summary>");
            sb.Append("        public List<Model.").Append(tableName).AppendLine("> GetModels(string strWhere, string orderby, int pageIndex, int pageSize)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetModels(strWhere, orderby, pageIndex, pageSize);");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 是否存在该记录 - void BuilderBllExists(StringBuilder sb)
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllExists(StringBuilder sb)
        {
            sb.AppendLine("        #region 是否存在该记录");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 是否存在该记录");
            sb.AppendLine("        /// </summary>");
            if (hasidentity)//自增&主键
            {
                sb.Append("        public bool Exists(").Append(identityType).Append(" ").Append(identityName).AppendLine(")//自增&主键");
                sb.AppendLine("        {");
                sb.Append("            return dal.Exists(").Append(identityName).AppendLine(");");
                sb.AppendLine("        }");
            }
            else//无主键
            {
                sb.Append("        public bool Exists(Model.").Append(tableName).AppendLine(" model)//自增主键 全无");
                sb.AppendLine("        {");
                sb.AppendLine("            return dal.Exists(model);");
                sb.AppendLine("        }");
            }
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 获取记录总数 - static void BuilderBllGetRecordCount(StringBuilder sb)
        /// <summary>
        /// 获取记录总数
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllGetRecordCount(StringBuilder sb)
        {
            sb.AppendLine("        #region 获取记录总数");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 获取记录总数");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public int GetRecordCount(string strWhere)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetRecordCount(strWhere);");
            sb.AppendLine("        }");
            sb.AppendLine("        public int GetRecordCount()");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetRecordCount(\"\");");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

        #region 得到最大的ID - void BuilderBllGetMaxId(StringBuilder sb)
        /// <summary>
        /// 得到最大的ID
        /// </summary>
        /// <param name="sb"></param>
        private void BuilderBllGetMaxId(StringBuilder sb)
        {
            sb.AppendLine("        #region 得到最大的ID");
            if (hasidentity)
            {

            }
            //else if (keys.Count != 0)//自增&主键
            //{

            //}
            else//无主键
            {
            }
            sb.AppendLine("        #endregion");
            sb.AppendLine();
        }
        #endregion

    }
}
